package com.liaoye.hellocustomview.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;

import com.liaoye.hellocustomview.R;

public class AvatarView extends View {

    private Bitmap mBitmap;
    private Paint mPaint;
    private Path mPath;

    public AvatarView(Context context) {
        super(context);
        init();
    }

    private void init() {
        //禁用硬件加速
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.avator);
        mPaint = new Paint();
        mPath = new Path();
        int width = mBitmap.getWidth();
        int height = mBitmap.getHeight();
        mPath.addCircle(width/2, height/2, width/2, Path.Direction.CCW);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        canvas.clipPath(mPath); //将画布裁剪为圆形
        canvas.translate(100, 100);

        canvas.drawBitmap(mBitmap, 0, 0, mPaint); //然后将头像绘制上去
        canvas.restore();
    }
}
